﻿(function($) {
    var 
    IE6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent),
    Timer, IE6FIX, Options;

    $.options_defaults = {
        elementID: 'notify',
        position: 'bottom', // 'top' or 'bottom'
        effect: 'slide', // 'slide', 'fade' or 'none'
        speed: 500,
        timeout: 5000,
        clickClose: true, // Click to close?
        closeID: 'closeNotify',
        isStopOver: true,
        onShow: function() { },
        onClose: function() { }
    }

    function close() {
        var 
      Obj = $("#" + Options.elementID),
      onClose = Obj.data('onClose'),
      call = function() {
          remove();
          if ($.isFunction(onClose))
              onClose();
      },
      spd = Obj.data('speed');

        switch (Obj.data('effect')) {
            case 'none':
                Obj.hide('', call);
            case 'fade':
                Obj.fadeOut(spd, call);
            default:
                Obj.slideUp(spd, call);
        }
    }

    function remove() {
        var obj = $("#" + Options.elementID);
        clearInterval(IE6FIX);
        clearTimeout(Timer);
        $(obj).find('div').unbind('click');
        $(obj).stop().hide();
    }

    // ugly support for an ugly browser
    function ie6TopFix() {
        var element = document.getElementById(Options.elementID);
        if (element) {
            element.style.top = parseInt(document.documentElement.scrollTop) + 'px';
        }
    }

    function ie6BottomFix() {
        var element = document.getElementById(Options.elementID);
        if (element) {
            element.style.top = parseInt(document.documentElement.scrollTop + document.documentElement.clientHeight - element.offsetHeight) + 'px';
        }
    }

    function show() {
        var container = $("#" + Options.elementID);
        if (Options.position == 'bottom')
            container.css('bottom', 5)
        else
            container.css('top', 5);

        container.css({ right: 5,
            position: IE6 ? 'absolute' : 'fixed',
            zIndex : 100
        });

        if (IE6) {
            if (Options.position == 'bottom') {
                container.css('bottom', 'auto');
                IE6FIX = setInterval(function() { ie6BottomFix() }, 50);
            } else {
                IE6FIX = setInterval(function() { ie6TopFix() }, 50);
            }
        }

        if (Options.close !== false) {
            $('#' + Options.closeID).click(function() {
                close();
            });
        }

        var start = function() {
            if (Options.timeout > 0)
                Timer = setTimeout(function() {
                    close();
                }, Options.timeout);
        }

        switch (Options.effect) {
            case 'none':
                container.show('', start);
            case 'fade':
                container.fadeIn(Options.speed, start);
            default:
                container.slideDown(Options.speed, start);
        }

        if (Options.clickClose)
            $("#" + Options.elementID).click(function() {
                close();
            });

        container.data({
            effect: Options.effect,
            speed: Options.speed,
            onClose: Options.onClose
        });

        if ($.isFunction(Options.onShow))
            Options.onShow();
            
        if (Options.isStopOver) {
            container.hover(function() {
                clearTimeout(Timer);
            }, start);
        }
    }

    $.notify = $.jN = function(opt) {
        opt = opt ? opt : {};

        Options = $.extend(true, {}, $.options_defaults, opt);

        remove();
        show();
    }

    $.notify.close = function() {
        close();
    }

    $.notify.version = 'v0.1';
})(jQuery);
